#!/bin/bash

# create_exp.sh
# wiSync
#
# Created by Youssef Francis on 12/9/07.
# Copyright 2007 iFrancis Development Team. All rights reserved.

# Creating ssh_authkey shell script and installing Native Apps

mecho1_run() {
loop1=0
while [ $loop1 -lt 4 ]
do
echo expect -exact '"Enter passphrase (empty for no passphrase):"' >> "${rsrcpath}/${exp_script}"
echo send '"\r"' >> "${rsrcpath}/${exp_script}"
loop1=$(($loop1 + 1))
done
}

mecho2_run() {
loop2=0
while [ $loop2 -lt 4 ]
do
echo expect -exact '"Enter same passphrase again:"' >> "${rsrcpath}/${exp_script}"
echo send '"\r"' >> "${rsrcpath}/${exp_script}"
loop2=$(($loop2 + 1))
done
}

expshell_run() {
echo expect -exact '"#"' >> "${rsrcpath}/${exp_script}"
}

return_run() {
loop3=0
while [ $loop3 -lt 4 ]
do
expshell_run
echo send '"\r"' >> "${rsrcpath}/${exp_script}"
loop3=$(($loop2 + 1))
done
}

sshexit_run() {
echo "expect eof" >> "${rsrcpath}/${exp_script}"
echo "expect -f '${rsrcpath}/${exp_script}'" > "${rsrcpath}/run_${exp_script}"
chmod +x "${rsrcpath}/${exp_script}"
chmod +x "${rsrcpath}/run_${exp_script}"
}

scpdo_run() {
echo "spawn scp $scpsend" > "${rsrcpath}/${exp_script}"
echo expect '"password:"' >> "${rsrcpath}/${exp_script}"
echo send '"'${rootpass_old}"\r"'"' >> "${rsrcpath}/${exp_script}"
echo expect eof >> "${rsrcpath}/${exp_script}"
echo "expect -f '${rsrcpath}/${exp_script}'" > "${rsrcpath}/run_${exp_script}"
chmod +x "${rsrcpath}/${exp_script}"
chmod +x "${rsrcpath}/run_${exp_script}"
}

# Checking if option to change iPhone root Password is ticked, and writing script to setup SSH Publickey Auth with the iPhone as a client

if [ -f ~/.ssh/known_hosts ]
then
mv ~/.ssh/known_hosts ~/.ssh/bknown_hosts
fi
if [ -f ~/.ssh/authorized_keys ]
then
mv ~/.ssh/authorized_keys ~/.ssh/bauthorized_keys
fi

exp_script=exp_ssh.sh
echo spawn ssh root@${phoneip} > "${rsrcpath}/${exp_script}"
echo expect '"(yes/no)?"' >> "${rsrcpath}/${exp_script}"
echo send '"yes\r"' >> "${rsrcpath}/${exp_script}"
echo expect '"password:"' >> "${rsrcpath}/${exp_script}"
echo send '"'${rootpass_old}"\r"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"'mv /private/var/root/.ssh/known_hosts /private/var/root/.ssh/bknown_hosts"\r"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"'mkdir /private/var/root/${istore}"\r"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"ssh-keygen -t dsa -f /private/var/root/.ssh/id_dsa\r"' >> "${rsrcpath}/${exp_script}"
mecho1_run
mecho2_run
expshell_run
echo send '"'"cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys\r"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"'exit"\r"'"' >> "${rsrcpath}/${exp_script}"
sshexit_run

# Checking if user has chosen to install wiToggle.app, and sending files via scp to iPhone
exp_script=exp_scp1.sh
scpsend="-r /Applications/wiSync/Resources/wiToggle.app root@${phoneip}:/Applications/"
scpdo_run

# Checking if user has chosen to install MobileFinder, and sending files via scp to iPhone
exp_script=exp_scp2.sh
scpsend="-r /Applications/wiSync/Resources/Finder.app root@${phoneip}:/Applications/"
case $nativeFinder in
0 ) echo "export nope=null" > "${rsrcpath}/run_${exp_script}" ;;
1 ) scpdo_run ;;
* ) chmod_run ;;
esac

# Taking shared publickey from iPhone and adding to Mac's authorized_keys file
exp_script=exp_scp3.sh
scpsend="root@${phoneip}:/private/var/root/.ssh/id_dsa.pub /tmp"
scpdo_run
echo "cat /tmp/id_dsa.pub >> ~/.ssh/authorized_keys" >> "${rsrcpath}/run_exp_scp3.sh"
echo "rm /tmp/id_dsa.pub" >> "${rsrcpath}/run_exp_scp3.sh"

# Sending executable scripts to iPhone
exp_script=exp_scp4.sh
scpsend="-r /Applications/wiSync/Resources/Scripts root@${phoneip}:/Applications/"
scpdo_run

# Sending alert binary to iPhone
exp_script=exp_scp5.sh
scpsend="/Applications/wiSync/Resources/alert root@${phoneip}:/Applications/"
scpdo_run

# Setting up selected crontab on iPhone, possibly forcing sync, and then forcing respring
exp_script=exp_ssh2.sh
echo "spawn ssh root@${phoneip}" > "${rsrcpath}/${exp_script}"
echo expect '"password:"' >> "${rsrcpath}/${exp_script}"
echo send '"'${rootpass_old}"\r"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"'crontab /Applications/Scripts/${crontab}"\r"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"'ssh -o PreferredAuthentications=publickey ${macuser}@${ipaddr}"\r"'"' >> "${rsrcpath}/${exp_script}"
echo expect -exact '"^Are you sure you want to continue connecting (yes/no)?$"' >> "${rsrcpath}/${exp_script}"
echo send '"yes\r"' >> "${rsrcpath}/${exp_script}"
echo expect '"$"' >> "${rsrcpath}/${exp_script}"
echo send '"'exit"\r"'"' >> "${rsrcpath}/${exp_script}"
return_run
if [ "${rootpass_edit_yn}" == 1 ]
then
expshell_run
echo send '"'passwd"\r"'"' >> "${rsrcpath}/${exp_script}"
echo expect '"New password:"' >> "${rsrcpath}/${exp_script}"
echo send '"'${rootpass_edit_string}"\r"'"' >> "${rsrcpath}/${exp_script}"
echo expect '"Retype new password:"' >> "${rsrcpath}/${exp_script}"
echo send '"'${rootpass_edit_string}"\r"'"' >> "${rsrcpath}/${exp_script}"
fi
expshell_run
read CRTAB < /Applications/wiSync/Resources/Scripts/wisync.cron
expshell_run
echo send '"'"echo "'`'crontab -l | grep -v rsync.sh'`'" > /Library/Preferences/com.iFrancis.wiSync-cron"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"'"echo $CRTAB >> /Library/Preferences/com.iFrancis.wiSync-cron\r"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"'"crontab /Library/Preferences/com.iFrancis.wiSync-cron\r"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"'killall SpringBoard"\r"'"' >> "${rsrcpath}/${exp_script}"
expshell_run
echo send '"'exit"\r"'"' >> "${rsrcpath}/${exp_script}"
sshexit_run